// pages/personal/personal.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info: "",
    hasUserInfo: false,
    id: ''
  },

  //退出登录
  sign_out() {
    var that = this
    //清除缓存
    wx.clearStorage()
    that.setData({
      hasUserInfo: false,
      user_info: "",
    })
  },

  login() {
    //商家登录 定时过期
    let date_now = new Date()
    let api_token = wx.getStorageSync('api_token')
    let c_date = wx.getStorageSync('c_date')
    let token_enadle = ((date_now - c_date) < 1800000)

    if (api_token && token_enadle) {
      wx.navigateTo({
        url: '/pages/shop_info/shop_info',
      })
    } else {
      wx.navigateTo({
        url: '/pages/merchant_login/merchant_login',
      })
    }
  },
  // 展示收藏商品
  show() {

  },


  search() {
    wx.navigateTo({
      url: '/pages/test/test',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    console.log(res, '88888888888888888888')
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    } else {
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length - 1] //获取当前页面的对象
      var url = currentPage.route //当前页面url
      console.log(url, '888888888888')
      console.log("来自右上角转发菜单")
      return {
        title: '分享至朋友圈',
        path: url,
        success: (res) => {
          console.log("转发成功", res);
        },
        fail: (res) => {
          console.log("转发失败", res);
        }
      }
    }

  },
  // 用户登录
  get_user_info() {
    let that = this
    wx.showModal({
      title: '登录',
      content: '您还没有登陆 是否现在登录',
      showCancel: true,
      cancelText: "取消",
      confirmText: "登录",
      confirmColor: '#dfb461',
      success: function (res) {
        if (res.cancel) {
          console.log("用户取消");
        } else {
          wx.getUserProfile({
            desc: '登录',
            success: (res) => {
              console.log(res);
              that.setData({
                user_info: res.userInfo,
              })
              let nick_name = res.userInfo.nickName
              let head = res.userInfo.avatarUrl
              wx.setStorageSync('user_info', res.userInfo)
              wx.setStorageSync('user_name', res.userInfo.nickName)

              wx.login({
                success: res => {
                  wx.request({
                    url: 'https://vip.join.ink/api/getweixinOpenid',
                    method: "POST",
                    data: {
                      code: res.code
                    },
                    method: "POST",
                    success: res => {
                      console.log(res);
                      wx.request({
                        url: 'https://vip.join.ink/api/login',
                        method: "POST",
                        data: {
                          openid: res.data.openid,
                          head,
                          nick_name
                        },
                        success: result => {
                          console.log(result.data.token);
                          if (result.data.token) {
                            that.setData({
                              hasUserInfo: true
                            })
                            wx.showToast({
                              title: '登陆成功',
                              icon: 'none'
                            })

                          } else(
                            wx.showToast({
                              title: '登陆失败请重新登录',
                              icon: 'none'
                            })
                          )
                          wx.setStorage({
                            data: result.data.token,
                            key: 'token',
                            success: (e) => {
                              let c_date_user = new Date()
                              wx.setStorageSync('user_id', result.data.user.id);
                              wx.setStorageSync('openid', res.data.openid)
                              wx.setStorageSync('c_date_user', c_date_user)
                              if (getApp().globalData.is_login_url) {
                                wx.navigateTo({
                                  url: '/pages/goods_info/goods_info?id=' + that.data.id,
                                })
                              }
                            }
                          })


                        }
                      })
                    }
                  })
                }
              })
            }
          })

        }
      },
    })







  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.data.id = getApp().globalData.id
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log(res)
      },
      fail(e) {
        console.log(e)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //校验缓存中的token 和 通过openid拿到的token进行比对
  check_token() {
    var that = this
    var user_info_token = wx.getStorageSync('token');
    //获取缓存中的openid或者是全局当中的openid变量 通过指定openid查询后台的token 
    wx.request({
      url: 'https://vip.join.ink/api/get_token',
      data: {
        openid: wx.getStorageSync('openid'), //获取缓存中的openid openid不会变
        token: user_info_token
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data.code == 200) {
          //此时重新设置新的token作为缓存对象
          wx.setStorageSync('token', res.data.api_token)
          wx.setStorageSync('token_is_agreement', res.data.data) //一致情况下为1
        } else {
          //缓存中的token和后台当中的token不一致  需要重新授权登录
          wx.removeStorageSync('token')
          if (user_info_token != '') {
            wx.showModal({
              title: '提示',
              content: 'token失效，请重新登录！',
              showCancel: false
            })
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/personal/personal',
              })
            }, 2000);
          }

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.check_token()
    let token = wx.getStorageSync('token')
    console.log(token, 'token');
    if (token) {
      this.setData({
        hasUserInfo: true
      })
    } else {
      this.setData({
        hasUserInfo: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})